﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_DSJCJ.DAL
{
    /// <summary>
    /// 业务逻辑类IPGroup 的摘要说明。
    /// </summary>
    public class IPGroup
    {

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="fIPGroupID">IPGroupID编号</param>
        /// <returns>得到一个对象实体</returns>
        public ElingOA6_DSJCJ.Model.IPGroup GetModel(int fIPGroupID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FIPGroupID,FEntityNo,FStartIP,FEndIP,FIPGroupName from t_IPGroup ");
            strSql.Append(" where FIPGroupID=@FIPGroupID");
            SqlParameter[] parameters = {
					new SqlParameter("@FIPGroupID", SqlDbType.Int,4)
			};
            parameters[0].Value = fIPGroupID;

            ElingOA6_DSJCJ.Model.IPGroup model = new ElingOA6_DSJCJ.Model.IPGroup();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="entity">公司编号</param>
        /// <returns>得到一个对象实体</returns>
        public DataSet GetModelByEntity(string entity)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FIPGroupID,FEntityNo,FStartIP,FEndIP,FIPGroupName from t_IPGroup ");
            strSql.Append(" where FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
			};
            parameters[0].Value = entity;

            ElingOA6_DSJCJ.Model.IPGroup model = new ElingOA6_DSJCJ.Model.IPGroup();
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="row">行</param>
        /// <returns>数据列表</returns>
        public ElingOA6_DSJCJ.Model.IPGroup DataRowToModel(DataRow row)
        {
            ElingOA6_DSJCJ.Model.IPGroup model = new ElingOA6_DSJCJ.Model.IPGroup();
            if (row != null)
            {
                if (row["FIPGroupID"] != null && row["FIPGroupID"].ToString() != "")
                {
                    model.FIPGroupID = int.Parse(row["FIPGroupID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FStartIP"] != null && row["FStartIP"].ToString() != "")
                {
                    model.FStartIP = long.Parse(row["FStartIP"].ToString());
                }
                if (row["FEndIP"] != null && row["FEndIP"].ToString() != "")
                {
                    model.FEndIP = long.Parse(row["FEndIP"].ToString());
                }
                if (row["FIPGroupName"] != null)
                {
                    model.FIPGroupName = row["FIPGroupName"].ToString();
                }
            }
            return model;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <param name="stratIP">开始IP字符串</param>
        /// <param name="endIP">结束IP字符串</param>
        /// <returns>是否更新</returns>
        public int Add(ElingOA6_DSJCJ.Model.IPGroup model, string stratIP, string endIP)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_IPGroup(");
            strSql.Append("FEntityNo,FStartIP,FEndIP,FIPGroupName)");
            strSql.Append(" values (");
            strSql.Append("@FEntityNo,[dbo].[fn_IP2Int](@FStartIP),[dbo].[fn_IP2Int](@FEndIP),@FIPGroupName)");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FStartIP", SqlDbType.NVarChar,20),
					new SqlParameter("@FEndIP", SqlDbType.NVarChar,20),
					new SqlParameter("@FIPGroupName", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = stratIP;
            parameters[2].Value = endIP;
            parameters[3].Value = model.FIPGroupName;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <param name="stratIP">开始IP字符串</param>
        /// <param name="endIP">结束IP字符串</param>
        /// <returns>是否更新</returns>
        public bool Update(ElingOA6_DSJCJ.Model.IPGroup model, string stratIP, string endIP)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_IPGroup set ");
            strSql.Append("FStartIP=[dbo].[fn_IP2Int](@FStartIP),");
            strSql.Append("FEndIP=[dbo].[fn_IP2Int](@FEndIP),");
            strSql.Append("FIPGroupName=@FIPGroupName");
            strSql.Append(" where FIPGroupID=@FIPGroupID");
            SqlParameter[] parameters = {
					new SqlParameter("@FStartIP", SqlDbType.NVarChar,20),
					new SqlParameter("@FEndIP", SqlDbType.NVarChar,20),
					new SqlParameter("@FIPGroupName", SqlDbType.NVarChar,20),
					new SqlParameter("@FIPGroupID", SqlDbType.Int,4),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = stratIP;
            parameters[1].Value = endIP;
            parameters[2].Value = model.FIPGroupName;
            parameters[3].Value = model.FIPGroupID;
            parameters[4].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="fIPGroupID">IPGroupID编号</param>
        /// <returns>是否删除</returns>
        public bool Delete(int fIPGroupID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_IPGroup ");
            strSql.Append(" where FIPGroupID=@FIPGroupID");
            SqlParameter[] parameters = {
					new SqlParameter("@FIPGroupID", SqlDbType.Int,4)
			};
            parameters[0].Value = fIPGroupID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 批量删除
        /// <creator>chenly</creator>
        /// <date>2013-9-7</date>
        /// </summary>
        /// <param name="adNoArray">编号数组</param>
        /// <param name="entityNo">公司编号</param>
        public void DelMany(string[] groupIDArray, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            if (groupIDArray.Length > 0)
            {
                //删除数据
                strSql = new StringBuilder();
                strSql.Append("delete from t_IPGroup ");
                strSql.Append(" where FIPGroupID=@FIPGroupID ");

                foreach (string groupID in groupIDArray)
                {
                    cmds = new CommandInfo();

                    SqlParameter[] parameters = {
					    new SqlParameter("@FIPGroupID", SqlDbType.Int,4)};

                    parameters[0].Value = groupID;

                    cmds.CommandText = strSql.ToString();
                    cmds.Parameters = parameters;
                    cmdList.Add(cmds);
                }

            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList);

        }


    }
}
